package com.example.wwk.graduationpractice.Service;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.SystemClock;

public class KeepAliveAndReconnectService extends Service {
    public KeepAliveAndReconnectService() {
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public IBinder onBind(Intent intent) {//服务启动时调用
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        new Thread(new Runnable() {
            @Override
            public void run() {//服务的任务逻辑

            }
        }).start();
        //定时方案,长时间在后台定时运行，每经过time时间便会启动执行一次
        AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
        int time = 30*1000; //30 秒
        long triggerAtTime = SystemClock.elapsedRealtime()+time;//系统开机至今经历的时间 + 任务定时的时间
        Intent i = new Intent(this,KeepAliveAndReconnectService.class);
        PendingIntent  pi = PendingIntent.getService(this,0,i,0);
        alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,triggerAtTime,pi);    //如果需要时间准确无误使用setExact()
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
